from flask import request

def init_config(app):
    app.config.from_pyfile('configs.py')

def init_cors(app):
    from flask_cors import CORS  # 跨域请求插件 https://www.cnblogs.com/cwp-bg/p/9256892.html
    CORS(app, supports_credentials = True)  # 允许跨域请求
    @app.after_request
    def after_request(response_):
        """
        #请求钩子，在所有的请求发生后执行，加入headers。
        :param response_:
        :return:
        """
        # response_ = make_response(response_)
        # import logging
        # logging.debug(app.config['DOMAIN_NAME'])

        origin = request.headers.get('Origin')
        if origin is None:
            origin = app.config['CORS_WHITE_LIST'][0]
        origin = origin.rstrip('/')
        if origin not in app.config['CORS_WHITE_LIST']:
            origin = app.config['CORS_WHITE_LIST'][0]
        # logging.debug(origin)
        response_.headers['Access-Control-Allow-Origin'] = origin  # 设置为'*'浏览器将不会发送cookies数据
        response_.headers['Access-Control-Allow-Methods'] = 'GET,POST'
        response_.headers['Access-Control-Allow-Headers'] = 'x-requested-with,content-type'
        response_.headers['Access-Control-Allow-Credentials'] = 'true'  # 是否允许后续请求携带认证信息（cookies）

        # print(response_.__class__)
        return response_


